package com.gexin.model.product;

import com.gexin.common.cache.CacheUtils;
import com.gexin.common.utils.SQLUtils;
import com.gexin.common.utils.StringUtil;
import com.gexin.model.product.base.BaseProductType;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * 业务名称:  t_product_type()
 */
@SuppressWarnings("serial")
public class ProductType extends BaseProductType<ProductType> {
	public static final ProductType dao = new ProductType();
	private static final String cache_key ="cache_key_product_type11";
	private static final String cache_key_All ="cache_key_product_type2222";
	private static final String cache_Top_key ="cache_key_product_type:top";
	public static final String cache_SearchClass_key ="cache_SearchClass_key";

	//商品类型管理
	public Page<ProductType> paginate(int pageNumber, int pageSize, ProductType types, String shop_ip){
		SQLUtils sql = new SQLUtils(" from t_product_type i left join t_shop_info s on i.shop_id=s.shop_id where 1=1 ");
		if(StringUtil.isNotBlank(types.getTypeName())){
			sql.whereLike("i.type_name",types.getTypeName());
		}
		if(types.getState()!=null){
			sql.whereEquals("i.state",types.getState());
		}
		if(StringUtil.isNotBlank(shop_ip)){
			sql.whereEquals("i.shop_id",shop_ip);
		}

		if(types.getPid()!=null){
			sql.whereEquals("i.pid",types.getPid());
		}else{
			sql.append(" and pid = 0 ");
		}
		sql.append(" and i.del_state=0 order by sort asc ");//排序
		return paginate(pageNumber,pageSize," select i.*,s.shop_name ", sql.toString());
	}

	//查询关联二级的状态
	public ProductType finByState(Integer id){
		return findFirst("select * from t_product_type where pid = ? and state = 0 and del_state=0 ",id);
	}
	public List<ProductType> findByPid(Integer id){
		return find(" select * from t_product_type where pid = ? and state=0 and del_state=0 order by sort asc ",id);
	}


	public List<ProductType>  getClassData(){
		List<ProductType>   list =  CacheUtils.get(cache_SearchClass_key);
		if(list==null){
			list = find("select  * from t_product_type  where shop_id='1' and state=0 and pid=0  and del_state=0 order by sort desc");
			for(ProductType t : list){
				t.put("sub",find("select  * from t_product_type  where  shop_id='1' and state=0  and pid=? and del_state=0  and image is not null order by sort desc ",t.getId()));
			}
			CacheUtils.set(cache_SearchClass_key, list,300);//缓存五分钟
		}

		return  list;
	}

	/**
	 * 查询全部  根据   shop_id  fang 2016-06-07
	 * 修改    2016-08-30    增加数目
	 * @return
	 */
	public List<ProductType> findAllByShopId(String shop_id){
		//return dao.find("select  *,() from t_product_type where shop_id=?  order  by sort asc",shop_id);
		return dao.find("select  t.*,(select count(*) from t_product p where p.type_id=t.id    )as product_num  from t_product_type t  where t.shop_id=? and t.state=0 and t.del_state=0 order  by t.sort asc",shop_id);
	}


	public List<ProductType>   findTypeByShopIdAndPid(String shop_id, Integer pid){
		return dao.find("select  * from t_product_type  where shop_id=? and pid=? and state =0 and del_state = 0",shop_id,pid);
	}
	/**
	 * 查询全部
	 * @return
	 */
	public List<ProductType> findAll(String type){
		SQLUtils sqlUtils = new SQLUtils("select  * from t_product_type where 1=1 ");
		sqlUtils.append(" and show_type!="+type);
		sqlUtils.append(" order by sort desc");
		String s = sqlUtils.toString();
		return dao.find("select  * from t_product_type order by sort desc");
	}

	/**
	 * 获取所有启用的缓存信息  显示图片用的
	 * @return  列表
	 */
	public   List<ProductType>  cache_GetEnableSubTypeList(Integer typeid){
		List<ProductType>   list = CacheUtils.get(cache_key+typeid);
		if(list==null){
			list =dao.find("select  id,type_name,type_desc,image from t_product_type where pid=? and state=0 and del_state=0 and image is not null   order by sort desc   limit 0,7 ",typeid);
			CacheUtils.set(cache_key+typeid,list,60*10);//缓存十分钟
		}
		return list;
	}

	/**
	 * 获取所有启用的缓存信息  获取所有的
	 * @return  列表
	 */
	public   List<ProductType>  cache_GetEnableSubTypeListAll(Integer typeid){
		List<ProductType>   list = CacheUtils.get(cache_key_All+typeid);
		if(list==null){
			list = dao.find("select  id,type_name from t_product_type where pid=? and state=0 and del_state=0  order by sort desc ",typeid);
			CacheUtils.set(cache_key_All+typeid,list,60*10);//缓存十分钟
		}
		return list;
	}




	/**
	 * 获取所有启用的缓存信息
	 * @return  列表
	 */
	public   List<ProductType>  cache_GetEnableList(Integer level){
		List<ProductType>   list = CacheUtils.getEhcache(cache_key);
		if(list==null){
			SQLUtils sql = new SQLUtils("select  id,type_name,type_desc,image from t_product_type where state=0 and del_state=0 ");
			if (level==1){
				sql.whereEquals("pid",level);
			}
			sql.append(" order by sort desc");
			list =dao.find(sql.toString());
			CacheUtils.setEhcache(cache_key, list);
		}
		return list;
	}

	public List<ProductType> getList(String shop_id, Integer pid, Integer state){
		SQLUtils sql = new SQLUtils("select t.* from t_product_type t where t.shop_id=? ");
		if (pid!=null){
			sql.whereEquals("t.pid",pid);
		}
		if (state!=null){
			sql.whereEquals("t.state",state);
		}
		sql.append(" and t.del_state=0 order by t.sort asc");
		sql.append(" limit 0,10 ");
		return find(sql.toString(),shop_id);
	}


	/**
	 * 获取所有顶级类分类
	 * @return
	 */
	public   List<ProductType>  cache_GetEnableTopList(){
		List<ProductType>   list = CacheUtils.getEhcache(cache_Top_key);
		if(list==null){
			list =dao.find("select  id,type_name,type_desc,image from t_product_type where state=0 and del_state=0 and pid=0  order by sort desc");
			CacheUtils.setEhcache(cache_Top_key, list);
		}
		return list;
	}

	public   List<ProductType>  cache_GetEnableTopList(String shop_id){
		List<ProductType>   list = CacheUtils.getEhcache(cache_Top_key);
		if(list==null){
			list =dao.find("select  id,type_name,type_desc,image from t_product_type where state=0 and del_state=0 and pid=0  and shop_id = ?  order by sort desc",shop_id);
			CacheUtils.setEhcache(cache_Top_key, list);
		}
		return list;
	}


	public void cache_clear(){
		CacheUtils.del(cache_SearchClass_key);
		CacheUtils.delEhcache(cache_key);
		CacheUtils.delEhcache(cache_Top_key);
	}
}
